﻿using Microsoft.VisualBasic.Logging;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BarCodeScanGunDemo
{
    /// <summary>
    /// 霍尼韦尔扫描枪操作类
    /// 扫码枪通用操作类
    /// </summary>
    public class BarCodeScanGun
    {
        #region 变量属性
        /// <summary>
        /// 串口
        /// </summary>
        private SerialPort serialPort = new SerialPort();
        /// <summary>
        /// 用于传递扫码结果
        /// </summary>
        public Action<string> ScanResult;
        #endregion

        /// <summary>
        /// 扫码枪操作类
        /// </summary>
        public BarCodeScanGun()
        {

        }

        /// <summary>
        /// 连接扫码枪
        /// </summary>
        /// <param name="portName">串口名称</param>
        /// <param name="baudRate">波特率</param>
        /// <param name="parity">校验位</param>
        /// <param name="dataBits">数据位</param>
        /// <param name="stopBits">停止位</param>
        /// <returns>结果</returns>
        public void ConnectScanner(string portName, int baudRate = 115200, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One)
        {
            if (serialPort.IsOpen)
            {
                serialPort.Close();
            }
            serialPort.PortName = portName; // 串口名称
            serialPort.BaudRate = baudRate; // 波特率
            serialPort.Parity = parity; // 校验位
            serialPort.DataBits = dataBits; // 数据位
            serialPort.StopBits = stopBits; // 停止位

            serialPort.ReceivedBytesThreshold = 1; // 接收字节阈值
            serialPort.DataReceived += SerialPort_DataReceived; // 绑定扫码条码接收事件

            serialPort.Open(); // 连接
        }

        /// <summary>
        /// 关闭连接
        /// </summary>
        public void DisConnect()
        {
            if (serialPort.IsOpen)
            {
                serialPort.Close();
            }
        }

        /// <summary>
        /// 开始扫码-控制手动的
        /// </summary>
        /// <param name="data"></param>
        public void StartScan(byte[] data)
        {
            //byte[] data = { 0x16, 0x54, 0x0d }; // 扫描开始指令
            serialPort.Encoding = Encoding.Unicode; // 设置编码格式-固定的
            serialPort.Write(data, 0, data.Length); // 发送指令
        }

        /// <summary>
        /// 停止扫码-控制手动的
        /// </summary>
        /// <param name="data"></param>
        public void StopScan(byte[] data)
        {
            //byte[] data = { 0x16, 0x55, 0x0d }; // 扫描开始指令
            serialPort.Encoding = Encoding.Unicode; // 设置编码格式-固定的
            serialPort.Write(data, 0, data.Length); // 发送指令
        }

        /// <summary>
        /// 扫描到的条码接收事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string result = string.Empty; // 条码
            serialPort.Encoding = Encoding.ASCII; // 设置接收编码格式
            result = serialPort.ReadExisting(); // 读取条码
            OnScanResult(result);
        }

        /// <summary>
        /// 执行返回检测结果委托
        /// </summary>
        /// <param name="barcode"></param>
        private void OnScanResult(string barcode)
        {
            if (ScanResult != null)
            {
                ScanResult(barcode);
            }
        }
    }
}
